package com.cszjzx.member.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.List;

import com.cszjzx.member.po.Field;


public class FieldDAOImpl implements FieldDAO ,RowMapper{
	private JdbcHelper helper;
	public FieldDAOImpl() throws IOException{
		helper=new JdbcHelper();
	}
	public void add(Field s) throws SQLException {
		String sql="call insertfield(?,?,?,?,?)";
		Object[]args={s.getId(),s.getName(),s.getChinese(),s.getStage(),s.getDefaultValue()};
		helper.executeUpdate(sql, args);
		helperAddToMember(s.getName());
		helperAddToTempleMember(s.getName());
	}
	//帮助add函数完成对member表的管理
    private void helperAddToMember(String string) throws SQLException{
    	String sql ="ALTER TABLE member add "+string +" varchar(255)";    //这个空格一定不能少
    	/*System.out.println(sql);*/
    	Object []args={};
    	helper.executeUpdate(sql, args);
    }
    //帮助add函数完成对templemember表的管理
    private void helperAddToTempleMember(String string) throws SQLException{
    	String sql ="ALTER TABLE TempleMember add "+string +" varchar(255)";    //这个空格一定不能少
    	/*System.out.println(sql);*/
    	Object []args={};
    	helper.executeUpdate(sql, args);
    }
	public void delete(String name) throws SQLException {
		String sql="delete from field where name=?";
		Object []args={name};
		helper.executeUpdate(sql, args);
		helperDeleteFromMemble(name);
		helperDeleteFromTempleMember(name);
	}
	//帮助delete函数完成对member表的字段删除
	private void helperDeleteFromMemble(String string) throws SQLException{
    	String sql ="ALTER TABLE member DROP "+string;    //这个空格一定不能少
    	/*System.out.println(sql);*/
    	Object []args={};
    	helper.executeUpdate(sql, args);
    }
	//帮助delete函数完成对templemember表的字段删除
	private void helperDeleteFromTempleMember(String string) throws SQLException{
    	String sql ="ALTER TABLE TempleMember DROP "+string;    //这个空格一定不能少
    	/*System.out.println(sql);*/
    	Object []args={};
    	helper.executeUpdate(sql, args);
    }
	public void update(Field field) throws SQLException {
		String sql ="UPDATE field SET  id=?,name=?,chinese=?,stage=?,defaultvalude=? where id=?";
		Object []args={field.getId(),field.getName(),field.getChinese(),field.getStage(),field.getDefaultValue(),field.getId()};
		helper.executeUpdate(sql, args);
	}
	@SuppressWarnings("unchecked")
	public List<Field> findAll() throws ClassNotFoundException, SQLException,IOException {
		String sql="select * from field order by id";
        Object[]args={};
		return helper.executeQuery(sql, args, new RowMapper() {
			public Object mapRow(ResultSet rs) throws SQLException, ClassNotFoundException, IOException {
				Field s = new Field();
				s.setId(rs.getInt("id"));
				s.setName(rs.getString("name"));
				s.setChinese(rs.getString("chinese"));
				s.setStage(rs.getInt("stage"));
				s.setDefaultValue(rs.getString("defaultvalue"));
				return s;
			}
		});	
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Field> findByStage(int stage) throws ClassNotFoundException, SQLException, IOException {
		String sql="select * from field  where stage=? order by id";
		Object[]args={stage};
		return helper.executeQuery(sql, args, this);
	}
	@Override
	public Object mapRow(ResultSet rs) throws SQLException, ClassNotFoundException, IOException {
		Field s = new Field();
		s.setId(rs.getInt("id"));
		s.setName(rs.getString("name"));
		s.setChinese(rs.getString("chinese"));
		s.setStage(rs.getInt("stage"));
		s.setDefaultValue(rs.getString("defaultvalue"));
		return s;
	}
	
	
}
